import logging

from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt

from djco.base.base_api import BaseApi
from djco.util import http_util
from djco.util.common_log import CommonLog

logger = logging.getLogger(__name__)

"""
    常用测试
"""


@csrf_exempt
def login(request, *args, **kwargs):
    print(request.body, args, kwargs)
    # todo 数据库查询
    param = request.POST
    admin = {'id': 1, 'account': param['account'], 'name': '张三'}
    BaseApi.cache_login_info(admin)
    result = BaseApi.login_info(1)  # 获取当前登录者信息
    return JsonResponse(BaseApi.api_ajax_success(result, "获取当前登录者信息"))


def get(request, *args, **kwargs):
    """
    GET 请求参数
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    print(request.GET, request.body, args, kwargs)
    return JsonResponse(BaseApi.api_ajax_success(request.GET, "GET请求参数"))


@csrf_exempt
def post(request, *args, **kwargs):
    """
    post 请求参数
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    print(request.POST, request.body, args, kwargs)
    return JsonResponse(BaseApi.api_ajax_success(request.POST, "POST请求参数"))


@csrf_exempt
def log_test(request, *args, **kwargs):
    """
    日志手动记录
    :param request:
    :param args:
    :param kwargs:
    :return:
    """
    CommonLog(logger).error("日志记录 error")
    CommonLog(logger).info("日志记录 info")
    CommonLog(logger).warning("日志记录 warning")
    return JsonResponse(BaseApi.api_ajax_success({}, "日志记录成功"))


def empty(val):
    if (val == None or val == '' or val == 0 or val == '0' or val.strip() == ''):
        return False
    return True
